<?php 

namespace app\admin\event;

use app\common\model\StoreProduct as StoreProductModel;
use app\common\utils\JsonService;

class StoreProductEvent{

    /*
    * 商品列表
    */
    public static function storeProductList(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where = [];
        if($id = isset($params['id'])){
                    $where[] = [['id','=',$params['id']]];
            }
            if($mer_id = isset($params['mer_id'])){
                    $where[] = [['mer_id','like',"%{$params['mer_id']}%"]];
            }
            if($store_name = isset($params['store_name'])){
                    $where[] = [['store_name','like',"%{$params['store_name']}%"]];
            }
            if($store_info = isset($params['store_info'])){
                    $where[] = [['store_info','like',"%{$params['store_info']}%"]];
            }
            if($keyword = isset($params['keyword'])){
                    $where[] = [['keyword','like',"%{$params['keyword']}%"]];
            }
            if($bar_code = isset($params['bar_code'])){
                    $where[] = [['bar_code','like',"%{$params['bar_code']}%"]];
            }
            if($cate_id = isset($params['cate_id'])){
                    $where[] = [['cate_id','like',"%{$params['cate_id']}%"]];
            }
            if($unit_name = isset($params['unit_name'])){
                    $where[] = [['unit_name','like',"%{$params['unit_name']}%"]];
            }
            if($sort = isset($params['sort'])){
                    $where[] = [['sort','like',"%{$params['sort']}%"]];
            }
            if($sales = isset($params['sales'])){
                    $where[] = [['sales','like',"%{$params['sales']}%"]];
            }
            if($stock = isset($params['stock'])){
                    $where[] = [['stock','like',"%{$params['stock']}%"]];
            }
            if($ficti = isset($params['ficti'])){
                    $where[] = [['ficti','like',"%{$params['ficti']}%"]];
            }
            if($browse = isset($params['browse'])){
                    $where[] = [['browse','like',"%{$params['browse']}%"]];
            }
            if($code_path = isset($params['code_path'])){
                    $where[] = [['code_path','like',"%{$params['code_path']}%"]];
            }
            if($soure_link = isset($params['soure_link'])){
                    $where[] = [['soure_link','like',"%{$params['soure_link']}%"]];
            }
             $condition = [
            'where' => $where,
            'order' => ['id' => 'desc'],
                'paginate' => [
                'page' => $page,
                'query' => $params,
            ],
        ];
        $storeProduct = StoreProductModel::getPageAll($condition);
        return $storeProduct;
    }


    /*
    * 新增商品
    */
    public static function createStoreProduct($data){

        try {
            StoreProductModel::create($data);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }

    /*
    * 更新商品
    */
    public static function updateStoreProduct($data){

        try {
            StoreProductModel::updateOne($data, ['id' => $data['id']]);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }


}

